﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CKFinder;
using System.Data;

public partial class Admin_EditDanhmucsanpham : System.Web.UI.Page
{
    private string id;
    DanhmucsanphamController danhmucsanphamController = new DanhmucsanphamController();
    protected void Page_Load(object sender, EventArgs e)
    {
        CKFinder.FileBrowser fb = new FileBrowser();
        fb.BasePath = "../ckfinder/";
        fb.SetupCKEditor(ckeditor);
        id = Request.QueryString["id"];
        
        if (!IsPostBack)
        {

            if (id == "" || id == null || string.IsNullOrWhiteSpace(id.ToString()) || string.IsNullOrEmpty(id.ToString()))
            {

                error();
            }
            else
            {

                int idd = Convert.ToInt32(id);
                if (!this.checkId(idd))
                {
                    error();
                }
                else
                {
                    DataTable dt = danhmucsanphamController.GetByID(idd);
                    Danhmucsanpham danhmucsanpham = new Danhmucsanpham(dt.Rows[0]);
                    txtName.Text = danhmucsanpham.Name;
                    txtOrder.Text = danhmucsanpham.Order + "";
                    txtTag.Text = danhmucsanpham.Tag;
                    txtUrl.Text = danhmucsanpham.Url;
                    ckeditor.Text = danhmucsanpham.Description;
                    if (danhmucsanpham.Status)
                    {
                        ddlStatus.SelectedIndex = 0;
                    }
                    else
                    {
                        ddlStatus.SelectedIndex = 1;
                    }

                }

            }
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Danhmucsanpham danhmucsanpham = new Danhmucsanpham();
        int idd = Convert.ToInt32(id);
        danhmucsanpham.DanhmucSanpham_id = idd;
        danhmucsanpham.Name = txtName.Text;
        danhmucsanpham.Url = txtUrl.Text;
        danhmucsanpham.Description = ckeditor.Text;
        danhmucsanpham.DateUpdate = DateTime.Now;
        danhmucsanpham.Order = Convert.ToInt32(txtOrder.Text);
        danhmucsanpham.Tag = txtTag.Text;


        if (ddlStatus.SelectedIndex == 0)
        {
            danhmucsanpham.Status = true;
        }
        else
        {
            danhmucsanpham.Status = false;
        }
        danhmucsanphamController.Update(danhmucsanpham);
        Response.Write("<script>alert('Cập nhật danh mục sản phẩm thành công')</script>");
        Server.Transfer(Request.RawUrl);
    }
    private bool checkId(int id)
    {
        DataTable dt = danhmucsanphamController.GetByID(id);

        if (dt.Rows.Count > 0)
        {
            return true;
        }
        return false;
    }
    private static void error()
    {
        HttpContext.Current.Response.Redirect("~/Error.aspx");
    }
}